<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: captcha.php 156 2013-09-22 07:52:52Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 156 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-22 07:52:52 +0000 (Sun, 22 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

include_once('includes/application_top.php');

if($_GET['show'] == 'true') {
	$first = $_SESSION['captcha_'.$_GET['name']]['first_num'];
	$second = $_SESSION['captcha_'.$_GET['name']]['second_num'];
	$operator = $_SESSION['captcha_'.$_GET['name']]['operator'];
		
	$captcha_w = 200;
	$captcha_h = 70;
	$min_font_size = 15;
	$max_font_size = 25;
	$angle = rand(10,30);
	$bg_size = 13;
	
	$img = imagecreate($captcha_w, $captcha_h);
	
	$black = imagecolorallocate($img, 0, 0, 0);
	$white = imagecolorallocate($img, 255, 255, 255);
	$grey = imagecolorallocate($img, 215, 215, 215);
	
	$fonts = array('includes/fonts/dustismo_italic.ttf',
					'includes/fonts/Dustismo_Roman_Italic_Bold.ttf',
					'includes/fonts/TheGirlNextDoor.ttf');
					
	$element = rand(0, count($fonts)-1);
	$font_path = $fonts[$element];
	
	imagefill($img, 0, 0, $white);	
	
	for($t = $bg_size; $t<$captcha_w; $t+=$bg_size)
		imageline($img, $t, 0, $t, $captcha_h, $grey);
	
	for($t = $bg_size; $t<$captcha_h; $t+=$bg_size)
		imageline($img, 0, $t, $captcha_w, $t, $grey);
	
	$item_space = $captcha_w/3;
	
	imagettftext($img, rand($min_font_size, $max_font_size), rand(-$angle, $angle),	rand(10,$item_space-20), rand(25, $captcha_h-25), $black, $font_path, $second);
		
	imagettftext($img, rand($min_font_size, $max_font_size), rand(-$angle, $angle), rand($item_space, 2*$item_space-20), rand(25, $captcha_h-25), $black, $font_path, $operator);
		
	imagettftext($img, rand($min_font_size, $max_font_size), rand(-$angle, $angle),rand( 2*$item_space, 3*$item_space-20), rand(25, $captcha_h-25), $black, $font_path, $first);
	
	header("Content-type:image/jpeg");
	header("Expires: -1");
	header("Cache-Control: post-check=0, pre-check=0");
	header("Pragma: no-cache");
	header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
	imagejpeg($img);
	imagedestroy($img);
	
} else {
		
	$is_xajax = (strlen($_POST['xajax']) > 4 || strlen($_POST['xjxfun']) > 4 || strlen($_GET['xajax']) > 4 || strlen($_GET['xjxfun']) > 4);
	
	if(!$is_xajax) {
		unset($_SESSION['security_code'.$captcha_site]);
		unset($_SESSION['captcha']);
		
		$operators = array('+','-','*');
		$operator = $operators[rand(0,2)];
		$first = rand(1,5);
		$second = rand(6,12);
		
		eval("\$sum = ".$second.$operator.$first.";");
		$_SESSION['security_code'.$captcha_site] = $sum;
		
		$_SESSION['captcha'.$captcha_site]['first_num'] = $first;
		$_SESSION['captcha'.$captcha_site]['second_num'] = $second;
		$_SESSION['captcha'.$captcha_site]['operator'] = $operator;
	}
}